define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require.async("resource/js/component/rules.js");
	var AJAX = require("resource/js/component/ajax.js");
	var A = require("resource/js/component/ajax.js");
	var C = require("resource/js/config/const.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var easycombobox = require("resource/js/component/easycombobox.js");
	easycombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"isCurrent"}
	})

	var push = undefined;
	//校验唯一性
	using(["validatebox"],function(){
		$.extend($.fn.validatebox.defaults.rules, {
			//校验工艺ID 和搅拌机编号是否存在记录
			machineNoResulAdd:{
				validator: function(value){
					var layerId = $("#layerId_add").combobox("getValue");
					var machineNo = $('#machineNo_add').val();
					var bol = false;
					if(layerId != null  && machineNo != null){
						AJAX.init().sync().success(function(code,msg,data){
							bol = data == "0" ? true : false;
						}).error(function(code,msg,data){
						}).post(BASEPATH+'/dcs/asphalt/device/queryAsphaltLayerIdMachineNo.htm',{"layerId":layerId,"machineNo":machineNo,"stationId":stationId});
					}
					return bol;
				},message: "存在重复编号对应工艺"
			},
			//校验工艺ID 和搅拌机编号是否存在记录
			machineNoResulUpd:{
				validator: function(value){
					var layerId = $("#layerId_upd").combobox("getValue");
					var machineNo = $('#machineNo_upd').val();
					var bol = false;
					if(push.machineNo == value && push.layerId == layerId){
						bol = true;
					}else{
						if(layerId != null  && machineNo != null){
							AJAX.init().sync().success(function(code,msg,data){
								bol = data == "0" ? true : false;
							}).error(function(code,msg,data){
							}).post(BASEPATH+'/dcs/asphalt/device/queryAsphaltLayerIdMachineNo.htm',{"layerId":layerId,"machineNo":machineNo,"stationId":stationId});
						}
					}
					return bol;
				},message: "存在重复编号对应工艺"
			}
		})
	})

	var dgutil = undefined;
	var config = {
		name:"沥青搅拌机设备设置表",
		column:[[
			{field:'id',title:'主键ID',width:100,align:'center',height:50,hidden:true},
		
			{field:'deviceName',title:'搅拌机名称',width:100,align:'center',height:50,hidden:false},
			
			{field:'machineNo',title:'控制机编码',width:100,align:'center',height:50,hidden:false},
		
			{field:'formulaName',title:'配方名称',width:100,align:'center',height:50,hidden:false},
		
			{field:'layerName',title:'结构层',width:100,align:'center',height:50,hidden:false},
		
			{field:'materialName',title:'混合料类型',width:100,align:'center',height:50,hidden:false},
			
			{field:'isCurrent',title:'是否上报',width:100,align:'center',height:50,formatter:function(value,row,index){
				if(value=="1"){
					return "<span style='color: red;cursor: pointer;'><input type='checkbox'  checked  class='isCurrent tab_style' value='0' layerId='"+row.layerId+"' machineNo = '"+row.machineNo+"'  status='"+value+"' id='"+row.id+"' ></input></span>";
				}else if(value=="0"){
					return "<span style='color: blue;cursor: pointer;'><input type='checkbox'   class='isCurrent tab_style' value='1' layerId='"+row.layerId+"' machineNo = '"+row.machineNo+"' status='"+value+"' id='"+row.id+"' ></input></span>";
				}
	        }},

		]]
	}

	var stationId = "",projectNo="",sectionNo="",stationNo="";
	function initPage(){
		stationId = $("#stationId_search").val();
		projectNo = $("#projectNo_search").val();
		sectionNo = $("#sectionNo_search").val();
		stationNo = $("#stationNo_search").val();
		dgutil = new easyutil.ajaxDataGrid(datagridconfig);
	}


	var initFiledByForm = function(rows,id){
		var validate = true;
		if(typeof rows == "boolean"){
			validate = rows;
		}
		easycombobox.init({ ele:"#isCurrent_"+id, grouptype:"isCurrent"});
		//初始化拌合站
//		easyutil.ajaxHandler(BASEPATH+"/dcs/asphalt/station/queryStationList.htm",null,function(code,msg,data){
//			easycombobox.init({ ele:"#stationId_"+id,valueField:'id',remote:false, textField:'stationNo'}).load(data);
//		});

		//初始化结构层
		easyutil.ajaxHandler(BASEPATH+'/dcs/sys/layer/querySysLayerList.htm',null,function(code,msg,data){
			if(data!=null&&data.length>0){
				easycombobox.init({ ele:"#layerId_"+id,valueField:'id',remote:true, textField:'layerName',prompt:'请选择结构层',
					onChange:function(rec){
						$("#materialId_"+id).combobox("clear");
						$("#materialId_"+id).combobox("loadData",[]);
						//初始化混合材料
						easyutil.ajaxHandler(BASEPATH+"/dcs/sys/material/queryMaterialListByLayerId.htm",{layerId:rec},function(code,msg,data){
							easycombobox.init({ ele:"#materialId_"+id,valueField:'id',remote:true, textField:'materialName'}).load(data);
						});
					},onLoadSuccess:function(){
						//$("#layerId_add").combobox("select",data[0].id);
					}
				}).load(data);
			}
		});
		easyutil.ajaxHandler(BASEPATH+"/dcs/sys/material/queryMaterialListByLayerId.htm",{layerId:$("#layerId_"+id).combobox("getValue")},function(code,msg,data){
			easycombobox.init({ele:"#materialId_"+id,valueField:'id',remote:true, textField:'materialName'}).load(data);
		});
	}
	
	var getRandom = function(){
		var hostName = window.location.hostname;
		if(hostName == 'localhost'){
			hostName = '127.0.0.1';
		}
		return hostName.replace(/\./g,'')+(new Date().getTime())+Math.round(Math.random()*10000);
	}

	//拌合参数添加
	var addDialogInit=function(baseUrl,row){
		$("#adddlg").dialog({
			onLoad:function(){
				$("#stationId_add").val($("#stationId").val());
				initFiledByForm(false,'add');
				$('#token_id_add').val(getRandom());
			},
			iconCls:"icon-save",
			title:"增加",
			href:baseUrl,
			buttons:[{
					text:"保存",
					iconCls:"icon-ok",
					accessKey:{akey:18,mkey:83},
					handler:function(){
						using("form",function(){
							if($("#addform1").form("validate")){
								if($("#addform2").form("validate")){
									var obj = $("#addform1").serializeJson();
									obj.stationId = stationId;
									obj.codeNum = projectNo+"-"+sectionNo+"-"+stationNo+"-"+obj.machineNo;
									var arr = ['0063','0125','025','05','1','2','4','5','63','8','10','125','14','16','20','25'];
									var arrPore = [];
									for (var i = 0; i < arr.length; i ++){
										var poreList = {};
										poreList.poreType = $('#'+arr[i]+'poreType_add').val();
										poreList.designValue = $('#'+arr[i]+'designValue_add').val();
										poreList.workMin = $('#'+arr[i]+'workMin_add').val();
										poreList.workMax = $('#'+arr[i]+'workMax_add').val();
										poreList.designMin = $('#'+arr[i]+'designMin_add').val();
										poreList.designMax = $('#'+arr[i]+'designMax_add').val();
										poreList.aggregate1 = $('#'+arr[i]+'aggregate1_add').val();
										poreList.aggregate2 = $('#'+arr[i]+'aggregate2_add').val();
										poreList.aggregate3 = $('#'+arr[i]+'aggregate3_add').val();
										poreList.aggregate4 = $('#'+arr[i]+'aggregate4_add').val();
										poreList.aggregate5 = $('#'+arr[i]+'aggregate5_add').val();
										poreList.aggregate6 = $('#'+arr[i]+'aggregate6_add').val();
										poreList.aggregate7 = $('#'+arr[i]+'aggregate7_add').val();
										poreList.aggregate8 = $('#'+arr[i]+'aggregate8_add').val();
										poreList.powder1 = $('#'+arr[i]+'powder1_add').val();
										poreList.powder2 = $('#'+arr[i]+'powder2_add').val();
										poreList.powder3 = $('#'+arr[i]+'powder3_add').val();
										poreList.powder4 = $('#'+arr[i]+'powder4_add').val();
										arrPore.push(poreList);
									}
									obj.arrPoreJson = JSON.stringify(arrPore);
									/*easyutil.ajaxHandler(BASEPATH + "/dcs/asphalt/device/saveMixAsphaltBlender.htm", obj, function () {
										dgutil.pager();
										$("#adddlg").dialog("close");
									});*/
									$.ajax({
										type: 'POST',
										url: BASEPATH+"/dcs/asphalt/device/saveMixAsphaltBlender.htm",
										data: obj,
										dataType: 'json',
										success: function (data) {
											dgutil.pager();
											if(data.msg.length > 0){
												$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
											}
											$("#adddlg").dialog("close");
										}
									});
								}
							}
						});
					}
					},
					{
						text:'取消',
						addClass:'l-btn-red',iconCls:'icon-not',
						accessKey:{akey:18,mkey:67},
						handler:function(){
							$("#adddlg").dialog("close");
						}
					}
			]
		});
		$("#adddlg").dialog("open");
	}

	//拌合参数修改
	var updDialogInit=function(baseUrl,row){
		$("#adddlg").dialog({
			iconCls:"icon-save",
			title:"修改",
			href:baseUrl,
			onLoad:function(){
				push = row;
				$("#stationId_upd").val($("#stationId").val());
				var poreList = row.poreList;
				$("#blendId_upd").val(row.id);
				$('#updform1').form('load',row);
				$("#layerId_upd").combobox("setValue",row.layerId);
				initFiledByForm(row,'upd');
//				$("#materialId_upd").combobox("setValue",row.materialId);
				AJAX.init().sync().success(function(code,msg,data){
					$('#updform2').form('load',data);
					var top = ['0063','0125','025','05','1','2','4','5','63','8','10','125','14','16','20','25'];
					var html="";

					for (var i = 0; i < top.length; i ++){
				/*		html = '<tr >'
							html += '<td><input type="hidden" id="'+top[i]+'asphaltPoreId_upd" name="id" value="'+data[i].id+'"></td>'
						    html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:3" id="'+top[i]+'poreType_upd" value="'+data[i].poreType+'" name="poreType" readonly="readonly"></td>'
						    html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'designValue_upd" value="'+data[i].designValue+'" name="designValue" maxlength="10" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'workMin_upd" value="'+data[i].workMin+'" name="workMin" maxlength="10" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'workMax_upd" value="'+data[i].workMax+'" name="workMax" maxlength="10" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'designMin_upd" value="'+data[i].designMin+'" name="designMin" maxlength="10" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'designMax_upd" value="'+data[i].designMax+'" name="designMax" maxlength="10" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate1_upd" value="'+data[i].aggregate1+'" name="aggregate1" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate2_upd" value="'+data[i].aggregate2+'" name="aggregate2" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate3_upd" value="'+data[i].aggregate3+'" name="aggregate3" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate4_upd" value="'+data[i].aggregate4+'" name="aggregate4" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate5_upd" value="'+data[i].aggregate5+'" name="aggregate5" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate6_upd" value="'+data[i].aggregate6+'" name="aggregate6" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate7_upd" value="'+data[i].aggregate7+'" name="aggregate7" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'aggregate8_upd" value="'+data[i].poreType+'" name="aggregate8" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'powder1_upd" value="'+data[i].poreType+'" name="powder1" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'powder2_upd" value="'+data[i].poreType+'" name="powder2" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'powder3_upd" value="'+data[i].poreType+'" name="powder3" maxlength="6" min="0" max="1"></td>'
							html += '<td><input type="text" class="easyui-numberbox num_style" data-options="precision:4" id="'+top[i]+'powder4_upd" value="'+data[i].poreType+'" name="powder4" maxlength="6" min="0" max="1"></td>'
						html += '</tr>'
*/
						$('#'+top[i]+'asphaltPoreId_upd').val(data[i].id);
						$('#'+top[i]+'poreType_upd').val(data[i].poreType);
						$('#'+top[i]+'designValue_upd').val(data[i].designValue);
						$('#'+top[i]+'workMin_upd').val(data[i].workMin);
						$('#'+top[i]+'workMax_upd').val(data[i].workMax);
						$('#'+top[i]+'designMin_upd').val(data[i].designMin);
						$('#'+top[i]+'designMax_upd').val(data[i].designMax);
						$('#'+top[i]+'aggregate1_upd').val(data[i].aggregate1);
						$('#'+top[i]+'aggregate2_upd').val(data[i].aggregate2);
						$('#'+top[i]+'aggregate3_upd').val(data[i].aggregate3);
						$('#'+top[i]+'aggregate4_upd').val(data[i].aggregate4);
						$('#'+top[i]+'aggregate5_upd').val(data[i].aggregate5);
						$('#'+top[i]+'aggregate6_upd').val(data[i].aggregate6);
						$('#'+top[i]+'aggregate7_upd').val(data[i].aggregate7);
						$('#'+top[i]+'aggregate8_upd').val(data[i].aggregate8);
						$('#'+top[i]+'powder1_upd').val(data[i].powder1);
						$('#'+top[i]+'powder2_upd').val(data[i].powder2);
						$('#'+top[i]+'powder3_upd').val(data[i].powder3);
						$('#'+top[i]+'powder4_upd').val(data[i].powder4);
					}

				}).post(BASEPATH+'/dcs/asphalt/pore/queryMixAsphaltPoreByBlenderId.htm',{blenderId:row.id});
				
				$('#token_id_upd').val(getRandom());	
				
			},
			buttons:[{
				text:"保存",
				iconCls:"icon-ok",
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using("form",function(){
						if($("#updform1").form("validate")) {
							if ($("#updform2").form("validate")) {
								var obj = $("#updform1").serializeJson();
								obj.stationId = stationId;
								obj.codeNum = projectNo+"-"+sectionNo+"-"+stationNo+"-"+obj.machineNo;
								var arr = ['0063','0125','025','05','1','2','4','5','63','8','10','125','14','16','20','25'];
								var arrPore = [];
								for (var i = 0; i < arr.length; i ++){
									var poreList = {};
									poreList.id = $('#'+arr[i]+'asphaltPoreId_upd').val();
									poreList.poreType = $('#'+arr[i]+'poreType_upd').val();
									poreList.designValue = $('#'+arr[i]+'designValue_upd').val();
									poreList.workMin = $('#'+arr[i]+'workMin_upd').val();
									poreList.workMax = $('#'+arr[i]+'workMax_upd').val();
									poreList.designMin = $('#'+arr[i]+'designMin_upd').val();
									poreList.designMax = $('#'+arr[i]+'designMax_upd').val();
									poreList.aggregate1 = $('#'+arr[i]+'aggregate1_upd').val();
									poreList.aggregate2 = $('#'+arr[i]+'aggregate2_upd').val();
									poreList.aggregate3 = $('#'+arr[i]+'aggregate3_upd').val();
									poreList.aggregate4 = $('#'+arr[i]+'aggregate4_upd').val();
									poreList.aggregate5 = $('#'+arr[i]+'aggregate5_upd').val();
									poreList.aggregate6 = $('#'+arr[i]+'aggregate6_upd').val();
									poreList.aggregate7 = $('#'+arr[i]+'aggregate7_upd').val();
									poreList.aggregate8 = $('#'+arr[i]+'aggregate8_upd').val();
									poreList.powder1 = $('#'+arr[i]+'powder1_upd').val();
									poreList.powder2 = $('#'+arr[i]+'powder2_upd').val();
									poreList.powder3 = $('#'+arr[i]+'powder3_upd').val();
									poreList.powder4 = $('#'+arr[i]+'powder4_upd').val();
									arrPore.push(poreList);
								}
								obj.arrPoreJson = JSON.stringify(arrPore);
								/*easyutil.ajaxHandler(BASEPATH + "/dcs/asphalt/device/updateMixAsphaltBlender.htm", obj, function () {
									dgutil.pager();
									$("#adddlg").dialog("close");
								});*/
								
								$.ajax({
									type: 'POST',
									url: BASEPATH+"/dcs/asphalt/device/updateMixAsphaltBlender.htm",
									data: obj,
									dataType: 'json',
									success: function (data) {
										dgutil.pager();
										if(data.msg.length > 0){
											$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
										}
										$("#adddlg").dialog("close");
									}
								});
								
							}
						}
					});
				}
			},
				{
					text:'取消',
					addClass:'l-btn-red',iconCls:'icon-not',
					accessKey:{akey:18,mkey:67},
					handler:function(){
						$("#adddlg").dialog("close");
					}
				}
			]
		});
		$("#adddlg").dialog("open");
	}
	
	var delInit = function(url,row){
		easyutil.ajaxHandler(url,{id:row.id},function(){
			dgutil.pager();
		});
	}

	var isCurrent = function(target){
		AJAX.init().success(function(code,msg,data){
			dgutil.pager();
		}).error(function(code,msg,data){
		}).post(BASEPATH+'/dcs/asphalt/device/updateIsCurrent.htm',{"machineNo":$(target).attr("machineNo"),"layerId":$(target).attr("layerId"),"isCurrent":$(target).attr("value"),"stationId":stationId});
	}
	
    exports.trans = function(){
    	var buttons={
			title:{
				_class:"",
				text:"拌和参数管理"
				
			},
			btn:[
			     {
			    	iconCls:"icon-add",
			    	text:"添加",
			    	/*addClass:"l-btn-display",*/
			    	url:"/view/dcs/asphalt/device/mixasphaltblenderadd",
			    	handler:addDialogInit
			     },
			     {
			    	 iconCls:"icon-edit",
				     text:"修改",
				     /*addClass:"l-btn-display",*/
				     url:"/view/dcs/asphalt/device/mixasphaltblenderupd",
				     needRow:true,
				     handler:updDialogInit
			     },
			     {
		    	 	iconCls: 'icon-remove',
					url:'/dcs/asphalt/device/deleteMixAsphaltBlender.htm',
					text:"删除",
					/*addClass:'l-btn-display',*/
					needRow:true,
					isCheck:true,
					handler: delInit
			     }
			     ]};
				datagridconfig={
					fitColumns:true,
					singleSelect:false,
					striped:true,
					autoRowHeight:false,
					fit:true,
					url:BASEPATH+"/dcs/asphalt/device/queryMixAsphaltBlenderByPage.htm",
					toolbar:buttons,
					columns:config.column,
					eid:"blenderdg",
					args:"searchform",
					onLoadSuccess:function(data){
						$(".isCurrent").bind('click',function(){
							isCurrent(this);
						});
					}
				};
				using(["datagrid","parser","textbox","numberbox","dialog","combobox","datetimebox","validatebox","messager"],function(){
					initPage();
					//按钮返回
				    $("#backBtn").on("click", function () {
				        $("#paramdlg").dialog("close");
				    });
				});
	}
});


